/**
 * File  : EventProcess.cc
 * Time  : 2014-12-22 10:25
 * Author: KashingLee
 * Ref   : Null
 */
#include "EventProcess.h"

EventProcess* EventProcess::ptr_ = 0;

Mutex EventProcess::mutex_;

EventProcess* EventProcess::GetInstance() {
  if (ptr_==0) {
    AutoMutex<Mutex> tmp(mutex_);
    if (ptr_ == 0) {
      ptr_ = new EventProcess();
    }
  }
  return ptr_;
}

EventProcess::EventProcess() {
  play_queue_    = new EventQueue<struct play_data>;
  setting_queue_ = new EventQueue<uint32_t>;
  check_queue_   = new EventQueue<uint32_t>;
  update_queue_  = new EventQueue<uint32_t>;
  ack_queue_     = new EventQueue<uint32_t>;
  net_log_queue_ = new EventQueue<struct LogType>;
  play_log_queue_    = new EventQueue<struct LogType>;
  set_log_queue_     = new EventQueue<struct LogType>;
  play_thread_   = new PlayThread;
  setting_thread_= new SettingThread;	
  check_thread_  = new CheckThread;
  update_thread_ = new UpdateThread;
  net_thread_		 = new NetThread;
	log_thread_		 = new LogThread;

  set_event_queue();

}

EventProcess::~EventProcess() {
  delete play_queue_    ;
  delete setting_queue_ ;
  delete check_queue_   ;
  delete update_queue_  ;
  delete ack_queue_     ;
	delete net_log_queue_ ;
	delete play_log_queue_;
	delete set_log_queue_ ;
  delete play_thread_   ;
  delete setting_thread_;
  delete check_thread_  ;
  delete update_thread_ ;
}

void EventProcess::Start (bool detached) {
  play_thread_   -> Start(detached);
  setting_thread_-> Start(detached);
  //check_thread_  -> Start(detached);
  //update_thread_ -> Start(detached);
	net_thread_    -> Start(detached);
	log_thread_    -> Start(detached);
}

void EventProcess::Join () {
 play_thread_   -> Join();
 setting_thread_-> Join();
 //check_thread_  -> Join();
 //update_thread_ -> Join();
 net_thread_    -> Join();
 log_thread_    -> Join();
}

void EventProcess::set_event_queue () {
  play_thread_   ->set_event_queue(play_queue_,    ack_queue_ , play_log_queue_);
  setting_thread_->set_event_queue(setting_queue_, ack_queue_ , set_log_queue_);
  //check_thread_  ->set_event_queue(check_queue_,   ack_queue_ , log_queue_);
  //update_thread_ ->set_event_queue(update_queue_,  ack_queue_ , log_queue_);
	net_thread_		 ->set_event_queue(play_queue_,    setting_queue_,  net_log_queue_);
	log_thread_    ->set_event_queue(set_log_queue_,    play_log_queue_,  net_log_queue_);
} 


uint32_t EventProcess::GetAck () {
  return ack_queue_->GetEvent()->opcode();
}
